package com.qinghe.pro.service.main.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qinghe.pro.entity.main.Role;
import com.qinghe.pro.entity.main.RolePermission;
import com.qinghe.pro.mapper.main.RoleMapper;
import com.qinghe.pro.mapper.main.RolePermissionMapper;
import com.qinghe.pro.service.main.RoleService;

@Service("roleServiceImpl")
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Override
	public List<Role> findAll() {
		List<Role> roles = roleMapper.selectAll();
		return roles;
	}

	@Override
	public Role get(Integer id) {
		Role role = roleMapper.selectById(id);
		return role;
	}

	@Override
	public void update(Role role) {
		rolePermissionMapper.deleteByRoleId(role.getId());
		for (RolePermission rolePermission : role.getRolePermissions()) {
			if (rolePermission.getPermission() != null) {
				rolePermission.setRoleId(role.getId());
				rolePermissionMapper.insert(rolePermission);
			}
		}
		roleMapper.updateByPrimaryKeySelective(role);
	}

}
